﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace APP
{
    public partial class PatchUserForm : Form
    {
        Record[] records=null;
        public PatchUserForm()
        {
            InitializeComponent();
        }

        private void bt_ok_Click(object sender, EventArgs e)
        {
            if (tb_suoxie.Text == string.Empty)
            {
                MessageBox.Show("用户缩写没填！");
                return;
            }

            string suoxie = tb_suoxie.Text;
            if (records == null || records.Length == 0)
            {
                MessageBox.Show("先载入要更新的yap文件！");
                return;
            }
            foreach(Record r in records)
            {
                String name = UserHelper.GetUserSuoxieByRecordNoVerified(r);
                r.ID = r.ID.Replace(name, suoxie);
                r.InputUserSuoXie = suoxie;
                List<Fact> _facts = r.Facts;
                foreach (Fact f in _facts)
                {
                    f.RecordNo = f.RecordNo.Replace(name, suoxie);
                }

                List<RecordResult> _result = r.Results;
                foreach (RecordResult re in _result)
                {
                    re.RecordNo = re.RecordNo.Replace(name, suoxie);
                }
            }
            RecordHelper.UpdateRecords(records);

            MessageBox.Show("更新完毕!");
            //刷回自己的yap
            string jiluPath = AppDomain.CurrentDomain.BaseDirectory + "记录\\";
            string YapFileName = jiluPath + UserSession.LoginUser.PingYing + ".yap";
            RecordHelper.RefreshDBOfRecord(YapFileName);
            this.Close();
        }

        private void bt_load_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "选择要加载的数据文件，数据文件以登录人姓名命名，如\"录入员\"用户的数据文件为录入员.yap。如果已经加载了，请点击取消。";
            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                RecordHelper.RefreshDBOfRecord(openFileDialog1.FileName);
                //IsMyDB = false;
                string currentDBPath = openFileDialog1.FileName;
                //btnQuery.PerformClick();
                //this.SetBtnColor(sender);
                //bt_readother.Text = "切回自己的数据库";
                //btnAddRecord.Enabled = false;
                //bt_loadData.Enabled = false;

                records = RecordHelper.GetAll();
                richTextBox1.Text += "共：" + records.Length.ToString()+"条记录。\n";
                foreach (Record r in records)
                {
                    richTextBox1.Text += r.ID + "\n";
                }
            }
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            if (records != null && records.Length > 0)
            {
                //刷回自己的yap
                string jiluPath = AppDomain.CurrentDomain.BaseDirectory + "记录\\";
                string YapFileName = jiluPath + UserSession.LoginUser.PingYing + ".yap";
                RecordHelper.RefreshDBOfRecord(YapFileName);
            }
        }
    }
}
